﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;

namespace CartoGraphic.Model
{
    class AbstractGeometry
    {
        public Pen pen;
        public Brush brush;
        public string info;
        public double width;
        public string type;
        public bool useBrush = false, isActive = false, isInput = false;
        public int dimention;
        public virtual void Draw(Graphics g)
        {

        }

        public AbstractGeometry()
        {
            pen = new Pen(Color.Black, 1);
            brush = new SolidBrush(Color.Black);
            this.info = "";
            this.width = 1;
        }


        public static bool isIntersect(double x0, double y0, double x1, double y1, double x2, double y2, double x3, double y3)
        {
            double x, y;
            double Minx01 = Math.Min(x0, x1);
            double Miny01 = Math.Min(y0, y1);
            double Minx23 = Math.Min(x2, x3);
            double Miny23 = Math.Min(y2, y3);
            double Maxx01 = Math.Max(x0, x1);
            double Maxy01 = Math.Max(y0, y1);
            double Maxx23 = Math.Max(x2, x3);
            double Maxy23 = Math.Max(y2, y3);

            if (x1 != x0 && x2 != x3)
            {
                double k1 = (y1 - y0) / (x1 - x0);
                double k2 = (y3 - y2) / (x3 - x2);
                double Den = (y1 - y0) * (x3 - x2) - (y3 - y2) * (x1 - x0);
                if (k1 == k2)
                {
                    double d1 = Math.Abs(y0 * (x1 - x0) - x0 * (y1 - y0) - y2 * (x3 - x2) + x2 * (y3 - y2));
                    if (d1 == 0)
                    {
                        if ((x2 > Minx01 && x2 < Maxy01 && y2 > Miny01 && y2 < Maxy01) || (x3 > Minx01 && x3 < Maxy01 && y3 > Miny01 && y3 < Maxy01)
                        || (x0 > Minx23 && x0 < Maxy23 && y0 > Miny23 && y0 < Maxy23) || (x1 > Minx23 && x1 < Maxy23 && y1 > Miny23 && y1 < Maxy23))
                        {
                            return true;
                        }
                        else
                        {
                            return false;
                        }
                    }
                    else
                    {
                        return false;
                    }
                }
                x = ((y2 - y0) * (x1 - x0) * (x3 - x2) + (y1 - y0) * (x3 - x2) * x0 - (y3 - y2) * (x1 - x0) * x2) / Den;
                y = ((y1 - y0) * (x - x0)) / (x1 - x0) + y0;

                if (Minx01 <= x && x <= Maxx01 && Miny01 <= y && y <= Maxy01 && Minx23 <= x && x <= Maxx23 && Miny23 <= y && y <= Maxy23)
                {
                    return true;
                }
            }
            else if (x1 == x0 && x2 != x3)
            {
                x = x0;
                y = ((y3 - y2) * (x0 - x2)) / (x3 - x2) + y2;
                if (Minx01 <= x && x <= Maxx01 && Miny01 <= y && y <= Maxy01 && Minx23 <= x && x <= Maxx23 && Miny23 <= y && y <= Maxy23)
                {
                    return true;
                }
            }
            else if (x1 != x0 && x2 == x3)
            {
                x = x2;
                y = ((y1 - y0) * (x2 - x0)) / (x1 - x0) + y0;
                if (Minx01 <= x && x <= Maxx01 && Miny01 <= y && y <= Maxy01 && Minx23 <= x && x <= Maxx23 && Miny23 <= y && y <= Maxy23)
                {
                    return true;
                }
            }
            return false;
        }
    }
}
